import oss2



class AliyunOSSBucket(object):
    """
    Model:
        满足阿里云云存储的算法逻辑

    Param:
        - env: odoo env

    Function:
        - get_oss_aliyun_enabled: 判断aliyun服务是否开启
        - get_oss_aliyun_connected: 判断aliyun服务是否连接
        - get_oss_aliyun_bucket: 创建阿里云存储对象
    """

    def __init__(self, env):
        self.env = env

    def get_oss_aliyun_enabled(self):
        """
        判断aliyun服务是否开启

        @return True/False
        """
        flag = self.env['ir.config_parameter'].sudo().get_param('aliyun_oss.enabled')
        return flag

    def get_oss_aliyun_connected(self):
        """
        判断aliyun服务是否连接

        @return True/False
        """
        flag = self.env['ir.config_parameter'].sudo().get_param('aliyun_oss.connected')
        return flag

    def get_aliyun_oss_bucket(self):
        """
        创建阿里云存储对象

        @return code(bucket)
        """
        try:
            icp = self.env['ir.config_parameter'].sudo()

            flag = icp.get_param('aliyun_oss.enabled')
            if not flag:
                return False

            Endpoint = icp.get_param('aliyun_oss.endpoint')
            bucket_name = icp.get_param('aliyun_oss.bucket_name')
            AccessKey_ID = icp.get_param('aliyun_oss.accessKey_id')
            AccessKey_Secret = icp.get_param('aliyun_oss.accessKey_secret')

            auth = oss2.Auth(AccessKey_ID, AccessKey_Secret)
            bucket = oss2.Bucket(auth, Endpoint, bucket_name)
            return bucket
        except:
            return False
